using Brick;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Assets.Scripts.GDK
{
	public class gdk_scene_manager
	{
		private static GameObject Scene;

		private static GameObject canvas;

		private static RectTransform GUILayer;

		private static List<string> sceneList = new List<string>();

		private static void initLayer()
		{
			canvas = GameObject.Find("Canvas");
			if (!canvas)
			{
				UnityEngine.Debug.Log("Canvas 节点不存在");
			}
			else
			{
				UnityEngine.Object.Destroy(canvas.GetComponent<gdk_canvas>());
				canvas.AddComponent<gdk_canvas>();
			}
			instantiateGUILayer();
			initLayerPanels();
			addInfo();
		}

		private static void instantiateGUILayer()
		{
			GUILayer = gdk_utils.NewPlane(canvas, "layer");
		}

		private static void initLayerPanels()
		{
			gdk_baseLayer<gdk_floorLayer>.Instance.setNode(instantiatePanel(UIBoxType.floorLayer, 1));
			gdk_baseLayer<gdk_menuLayer>.Instance.setNode(instantiatePanel(UIBoxType.menuLayer, 3));
			gdk_baseLayer<gdk_panelLayer>.Instance.setNode(instantiatePanel(UIBoxType.panelLayer, 5));
			gdk_baseLayer<gdk_popMenuLayer>.Instance.setNode(instantiatePanel(UIBoxType.popMenuLayer, 6));
			gdk_baseLayer<gdk_settingLayer>.Instance.setNode(instantiatePanel(UIBoxType.settingLayer, 7));
			gdk_baseLayer<gdk_alertLayer>.Instance.setNode(instantiatePanel(UIBoxType.alertLayer, 8));
			gdk_baseLayer<gdk_messageLayer>.Instance.setNode(instantiatePanel(UIBoxType.messageLayer, 9));
			gdk_baseLayer<gdk_guideLayer>.Instance.setNode(instantiatePanel(UIBoxType.guideLayer, 10));
			gdk_baseLayer<gdk_tipLayer>.Instance.setNode(instantiatePanel(UIBoxType.tipLayer, 11));
			gdk_baseLayer<gdk_waitingLayer>.Instance.setNode(instantiatePanel(UIBoxType.waitingLayer, 12));
			gdk_baseLayer<gdk_loadingLayer>.Instance.setNode(instantiatePanel(UIBoxType.loadingLayer, 13));
			gdk_baseLayer<gdk_systemPopLayer>.Instance.setNode(instantiatePanel(UIBoxType.systemPopLayer, 14));
			gdk_baseLayer<gdk_debugLayer>.Instance.setNode(instantiatePanel(UIBoxType.debugLayer, 15));
			gdk_baseLayer<gdk_logLayer>.Instance.setNode(instantiatePanel(UIBoxType.logLayer, 16));
		}

		private static GameObject instantiatePanel(string name, int index = 0)
		{
			GameObject gameObject = gdk_utils.NewPlane(GUILayer.gameObject, name).gameObject;
			gameObject.GetComponent<RectTransform>().position = new Vector3(0f, 0f, -index * 20);
			return gameObject;
		}

		private static void addInfo()
		{
			GameObject gameObject = gdk_utils.NewPlane(GUILayer.gameObject, "gdk_info").gameObject;
			gameObject.AddComponent<gdk_info>();
		}

		public static GameObject getScene()
		{
			if (Scene == null)
			{
				UnityEngine.Debug.Log("禁止在Awake未完成时获取Scene=Null");
				return null;
			}
			return Scene;
		}

		public static void loadScene(string sceneName)
		{
			Scene = null;
			UnityEngine.Debug.Log(">>>Canvas loadScene scene " + sceneName);
			sceneList.Remove(sceneName);
			sceneList.Add(sceneName);
			SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
		}

		public static void blackScene()
		{
			Scene = null;
            sceneList.RemoveAt(sceneList.Count - 1);
            if (sceneList.Count <= 0)
			{
				loadScene(0);
				return;
			}			
			string sceneName = sceneList[sceneList.Count - 1];           
            SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
		}

		public static void loadScene(int sceneIndex)
		{
			Scene = null;
			UnityEngine.Debug.Log(">>>Canvas loadScene sceneIndex " + sceneIndex);
			SceneManager.LoadScene(sceneIndex, LoadSceneMode.Single);
		}

		public static void loadEndScene(GameObject _scene)
		{
			Scene = _scene;
			initLayer();
			Application.targetFrameRate = 60;
			UnityEngine.Debug.Log("Canvas initLayer");
		}

		public static void addPanel(GameObject item)
		{
			gdk_baseLayer<gdk_panelLayer>.Instance.show(item);
		}

		public static void delPanel(GameObject item)
		{
			gdk_baseLayer<gdk_panelLayer>.Instance.hide(item);
		}

		public static void addSetting(GameObject item)
		{
			gdk_baseLayer<gdk_settingLayer>.Instance.show(item);
		}

		public static void delSetting(GameObject item)
		{
			gdk_baseLayer<gdk_settingLayer>.Instance.hide(item);
		}

		public static void addMessage(GameObject item)
		{
			gdk_baseLayer<gdk_messageLayer>.Instance.show(item);
		}

		public static void delMessage(GameObject item)
		{
			gdk_baseLayer<gdk_messageLayer>.Instance.hide(item);
		}

		public static void addAlert(GameObject item)
		{
			gdk_baseLayer<gdk_alertLayer>.Instance.show(item);
		}

		public static void delAlert(GameObject item)
		{
			gdk_baseLayer<gdk_alertLayer>.Instance.hide(item);
		}

		public static void addTip(GameObject item)
		{
			gdk_baseLayer<gdk_tipLayer>.Instance.show(item);
		}

		public static void delTip(GameObject item)
		{
			gdk_baseLayer<gdk_tipLayer>.Instance.hide(item);
		}

		public static void addLog(string str, float time)
		{
			gdk_baseLayer<gdk_logLayer>.Instance.log(str, time);
		}

		public static void addDebug(GameObject item)
		{
			gdk_baseLayer<gdk_debugLayer>.Instance.show(item);
		}

		public static void delDebug(GameObject item)
		{
			gdk_baseLayer<gdk_debugLayer>.Instance.hide(item);
		}

		public static void addSystemUI(GameObject item)
		{
			gdk_baseLayer<gdk_systemPopLayer>.Instance.show(item);
		}

		public static void delSystemUI(GameObject item)
		{
			gdk_baseLayer<gdk_systemPopLayer>.Instance.hide(item);
		}

		public static void addLoading(GameObject item)
		{
			gdk_baseLayer<gdk_loadingLayer>.Instance.show(item);
		}

		public static void delLoading(GameObject item)
		{
			gdk_baseLayer<gdk_loadingLayer>.Instance.hide(item);
		}

		public static void addGuide(GameObject item)
		{
			gdk_baseLayer<gdk_guideLayer>.Instance.show(item);
		}

		public static void delGuide(GameObject item)
		{
			gdk_baseLayer<gdk_guideLayer>.Instance.hide(item);
		}
	}
}
